<template>
  <el-drawer class="drawer-container" :visible.sync="visible" title="分配权限" append-to-body size="800px">
    <div class="drawer-search">
      <el-form ref="queryForm" inline :model="queryParams" size="small">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="queryParams.roleName" clearable placeholder="请输入角色名称"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="角色状态" prop="roleStatus">
          <el-select v-model="queryParams.roleStatus" clearable placeholder="角色状态">
            <el-option v-for="item in localDict.role_status" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="drawer-content">
      <div class="drawer-operate">
        <el-button icon="el-icon-plus" plain size="mini" type="danger" @click="handleAdd">添加授权</el-button>
        <el-button :disabled="multiple" icon="el-icon-delete" plain size="mini" type="danger" @click="handleDelete">
          取消授权
        </el-button>
      </div>
      <div class="drawer-data">
        <el-table ref="table" v-loading="loading" row-key="id" :data="dataList" @row-click="handleClickRow"
                  @selection-change="handleSelectionChange">
          <el-table-column :reserve-selection="true" type="selection" width="55"></el-table-column>
          <el-table-column align="center" label="角色编号" prop="role.code"/>
          <el-table-column align="center" label="角色名称" prop="role.name"/>
          <el-table-column align="center" label="角色类型" prop="role.type">
            <template slot-scope="scope">
              <dict-tag :options="localDict.role_type" :key="scope.row.role.type"/>
            </template>
          </el-table-column>
          <el-table-column align="center" label="角色状态" prop="role.status">
            <template slot-scope="scope">
              <dict-tag :options="localDict.role_status" :key="scope.row.role.status"/>
            </template>
          </el-table-column>
          <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
            <template slot-scope="scope">
              <el-button icon="el-icon-delete" size="mini" type="text" @click="handleDelete(scope.row)">取消授权
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="pageParam.total > 0" :total="pageParam.total" :page.sync="pageParam.pageNum"
                    :limit.sync="pageParam.pageSize"/>
      </div>
    </div>
    <select-role-card ref="selected" border :page-size="5" show-search/>
  </el-drawer>
</template>

<script>
import {list} from "@/api/base";
import SelectRoleCard from "../../role/components/SelectCard.vue";
import {getStaticData} from "../../../../api/common";

export default {
  name: "AuthModel",
  components: {SelectRoleCard},
  data() {
    return {
      ids: [],
      multiple: true,
      title: '',
      visible: false,
      // 遮罩层
      loading: true,
      userId: undefined,
      baseUrl: '/system/user/role/',
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      queryParams: {},
      localDict: {
        role_type: [],
        role_scope: [],
        role_status: [],
      }
    };
  },
  created() {
    this.getLocalDict()
  },
  methods: {
    async getLocalDict() {
      await getStaticData('system-role-type').then(res => {
        this.localDict.role_type = res.data
      })
      await getStaticData('system-role-scope').then(res => {
        this.localDict.role_scope = res.data
      })
      await getStaticData('system-role-status').then(res => {
        this.localDict.role_status = res.data
      })
    },
    getList() {
      this.loading = true
      list(this.baseUrl, {
        userId: this.userId,
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then((response) => {
        console.log(this.localDict)
        this.dataList = response.rows;
        this.pageParam.total = response.total;
        this.loading = false
      })
    },
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList()
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery()
    },
    /** 单击选中行数据 */
    handleClickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = this.ids.length !== 0
    },
    handleAdd() {
      this.$refs.selected.validate = true;
      this.$refs.selected.resetQuery()
    },
    handleDelete(row) {

    },
    /** 提交按钮 */
    handleSubmit() {
      // const userId = this.form.userId;
      // const roleIds = this.roleIds.join(",");
      // updateAuthRole({userId: userId, roleIds: roleIds}).then((response) => {
      //   this.$modal.msgSuccess("授权成功");
      //   this.close();
      // });
    },
  },
};
</script>
